<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>gears.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_gears.js.html">gears.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This file contains functions for using Gears.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>// TODO(arv): The Gears team is planning to inject the Gears factory as
<a name="line20"></a>// google.gears.factory in the main thread as well. Currently it is only
<a name="line21"></a>// injected in the worker threads.
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.gears&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.string&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Returns a new Gears factory object.
<a name="line31"></a> * @return {Object} the Gears factory object if available or null otherwise.
<a name="line32"></a> */
<a name="line33"></a>goog.gears.getFactory = function() {
<a name="line34"></a>  if (goog.gears.factory_ != undefined) {
<a name="line35"></a>    return goog.gears.factory_;
<a name="line36"></a>  }
<a name="line37"></a>
<a name="line38"></a>  // In the worker threads Gears injects google.gears.factory. They also plan
<a name="line39"></a>  // to do this in the main thread.
<a name="line40"></a>  var factory = goog.getObjectByName(&#39;google.gears.factory&#39;);
<a name="line41"></a>  if (factory) {
<a name="line42"></a>    return goog.gears.factory_ = factory;
<a name="line43"></a>  }
<a name="line44"></a>
<a name="line45"></a>  // Mozilla
<a name="line46"></a>  /** @preserveTry */
<a name="line47"></a>  try {
<a name="line48"></a>    var gearsFactory = /** @type {Function} */
<a name="line49"></a>        (goog.getObjectByName(&#39;GearsFactory&#39;));
<a name="line50"></a>    return goog.gears.factory_ = new gearsFactory;
<a name="line51"></a>  } catch (ex) {
<a name="line52"></a>    // fall through
<a name="line53"></a>  }
<a name="line54"></a>
<a name="line55"></a>  // MSIE
<a name="line56"></a>  /** @preserveTry */
<a name="line57"></a>  try {
<a name="line58"></a>    factory = new ActiveXObject(&#39;Gears.Factory&#39;);
<a name="line59"></a>    var buildInfo = factory.getBuildInfo();
<a name="line60"></a>    if (buildInfo.indexOf(&#39;ie_mobile&#39;) != -1) {
<a name="line61"></a>      factory.privateSetGlobalObject(goog.global);
<a name="line62"></a>    }
<a name="line63"></a>    return goog.gears.factory_ = factory;
<a name="line64"></a>  } catch (ex) {
<a name="line65"></a>    // fall through
<a name="line66"></a>  }
<a name="line67"></a>
<a name="line68"></a>  return goog.gears.factory_ = goog.gears.tryGearsObject_();
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Attempt to create a factory by adding an object element to the DOM.
<a name="line74"></a> * This covers the case for safari.
<a name="line75"></a> * @return {Function} The factory, or null.
<a name="line76"></a> * @private
<a name="line77"></a> */
<a name="line78"></a>goog.gears.tryGearsObject_ = function() {
<a name="line79"></a>  // HACK(arv): Use square bracket notation so this can compile in an
<a name="line80"></a>  // environment without a DOM.
<a name="line81"></a>  var win = goog.getObjectByName(&#39;window&#39;);
<a name="line82"></a>  // Safari
<a name="line83"></a>  if (win &amp;&amp; win[&#39;navigator&#39;][&#39;mimeTypes&#39;][&#39;application/x-googlegears&#39;]) {
<a name="line84"></a>    /** @preserveTry */
<a name="line85"></a>    try {
<a name="line86"></a>      var doc = win[&#39;document&#39;];
<a name="line87"></a>      var factory = doc[&#39;getElementById&#39;](&#39;gears-factory&#39;);
<a name="line88"></a>
<a name="line89"></a>      // If missing, create a place for it
<a name="line90"></a>      if (!factory) {
<a name="line91"></a>        factory = doc[&#39;createElement&#39;](&#39;object&#39;);
<a name="line92"></a>        factory[&#39;style&#39;][&#39;display&#39;] = &#39;none&#39;;
<a name="line93"></a>        factory[&#39;width&#39;] = &#39;0&#39;;
<a name="line94"></a>        factory[&#39;height&#39;] = &#39;0&#39;;
<a name="line95"></a>        factory[&#39;type&#39;] = &#39;application/x-googlegears&#39;;
<a name="line96"></a>        factory[&#39;id&#39;] = &#39;gears-factory&#39;;
<a name="line97"></a>        doc[&#39;documentElement&#39;][&#39;appendChild&#39;](factory);
<a name="line98"></a>      }
<a name="line99"></a>
<a name="line100"></a>      // Even if Gears failed to get created we get an object element. Make
<a name="line101"></a>      // sure that it has a create method before assuming it is a Gears factory.
<a name="line102"></a>      if (typeof factory.create != &#39;undefined&#39;) {
<a name="line103"></a>        return factory;
<a name="line104"></a>      }
<a name="line105"></a>    } catch (ex) {
<a name="line106"></a>      // fall through
<a name="line107"></a>    }
<a name="line108"></a>  }
<a name="line109"></a>  return null;
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Cached result of getFactory
<a name="line115"></a> * @type {Object|undefined}
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>goog.gears.factory_ = undefined;
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Whether we can create a Gears factory object. This does not cache the
<a name="line123"></a> * result.
<a name="line124"></a> * @return {boolean} true if we can create a Gears factory object.
<a name="line125"></a> */
<a name="line126"></a>goog.gears.hasFactory = function() {
<a name="line127"></a>  if (goog.gears.hasFactory_ != undefined) return goog.gears.hasFactory_;
<a name="line128"></a>
<a name="line129"></a>  // Use [] notation so we don&#39;t have to put this in externs
<a name="line130"></a>  var factory = goog.getObjectByName(&#39;google.gears.factory&#39;);
<a name="line131"></a>  if (factory || goog.getObjectByName(&#39;GearsFactory&#39;)) {
<a name="line132"></a>    return goog.gears.hasFactory_ = true;
<a name="line133"></a>  }
<a name="line134"></a>
<a name="line135"></a>  // Try ActiveXObject for IE
<a name="line136"></a>  if (typeof ActiveXObject != &#39;undefined&#39;) {
<a name="line137"></a>    /** @preserveTry */
<a name="line138"></a>    try {
<a name="line139"></a>      new ActiveXObject(&#39;Gears.Factory&#39;);
<a name="line140"></a>      return goog.gears.hasFactory_ = true;
<a name="line141"></a>    } catch (ex) {
<a name="line142"></a>      return goog.gears.hasFactory_ = false;
<a name="line143"></a>    }
<a name="line144"></a>  }
<a name="line145"></a>
<a name="line146"></a>  // NOTE(user): For safari we have to actually create an object element
<a name="line147"></a>  // in the DOM. We have to do it in hasFactory so we can reliably know whether
<a name="line148"></a>  // there actually is a factory, else we can get into a situation, e.g. in
<a name="line149"></a>  // FF 3.5.3 where the Gears add-on is disabled because it&#39;s incompatible
<a name="line150"></a>  // with FF but application/x-googlegears is still in the mimeTypes object.
<a name="line151"></a>  //
<a name="line152"></a>  // HACK(arv): Use object by name so this can compile in an environment without
<a name="line153"></a>  // a DOM.
<a name="line154"></a>  var mimeTypes = goog.getObjectByName(&#39;navigator.mimeTypes&#39;);
<a name="line155"></a>  if (mimeTypes &amp;&amp; mimeTypes[&#39;application/x-googlegears&#39;]) {
<a name="line156"></a>    factory = goog.gears.tryGearsObject_();
<a name="line157"></a>
<a name="line158"></a>    if (factory) {
<a name="line159"></a>      // Might as well cache it while we have it.
<a name="line160"></a>      goog.gears.factory_ = factory;
<a name="line161"></a>      return goog.gears.hasFactory_ = true;
<a name="line162"></a>    }
<a name="line163"></a>  }
<a name="line164"></a>
<a name="line165"></a>  return goog.gears.hasFactory_ = false;
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Cached result of hasFactory.
<a name="line171"></a> * @type {boolean|undefined}
<a name="line172"></a> * @private
<a name="line173"></a> */
<a name="line174"></a>goog.gears.hasFactory_ = undefined;
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>/**
<a name="line178"></a> * Maximum file name length.
<a name="line179"></a> * @type {number}
<a name="line180"></a> * @private
<a name="line181"></a> */
<a name="line182"></a>goog.gears.MAX_FILE_NAME_LENGTH_ = 64;
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Allow 10 characters for hash (#goog.string.hashCode).
<a name="line187"></a> * @type {number}
<a name="line188"></a> * @private
<a name="line189"></a> */
<a name="line190"></a>goog.gears.MAX_FILE_NAME_PREFIX_LENGTH_ = goog.gears.MAX_FILE_NAME_LENGTH_ - 10;
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a>  * Gears only allows file names up to 64 characters, so this function shortens
<a name="line195"></a>  * file names to fit in this limit. #goog.string.hashCode is used to compress
<a name="line196"></a>  * the name. This also removes invalid characters.
<a name="line197"></a>  * @param {string} originalFileName Original (potentially unsafe) file name.
<a name="line198"></a>  * @return {string} Safe file name. If originalFileName null or empty,
<a name="line199"></a>  *     return originalFileName.
<a name="line200"></a>  * @throws {Error} If originalFileName is null, empty or contains only
<a name="line201"></a>  *     invalid characters.
<a name="line202"></a>  */
<a name="line203"></a>goog.gears.makeSafeFileName = function(originalFileName) {
<a name="line204"></a>  if (!originalFileName) {
<a name="line205"></a>    throw Error(&#39;file name empty&#39;);
<a name="line206"></a>  }
<a name="line207"></a>
<a name="line208"></a>  // Safety measure since Gears behaves very badly if it gets an unexpected
<a name="line209"></a>  // data type.
<a name="line210"></a>  originalFileName = String(originalFileName);
<a name="line211"></a>
<a name="line212"></a>  // TODO(user): This should be removed when the Gears code
<a name="line213"></a>  // gets fixed to allow for any id to be passed in. Right now
<a name="line214"></a>  // it fails to create a user specific database if the characters
<a name="line215"></a>  // sent in are non alphanumeric.
<a name="line216"></a>  var sanitizedFileName = originalFileName.replace(/[^a-zA-Z0-9\.\-@_]/g, &#39;&#39;);
<a name="line217"></a>  if (!sanitizedFileName) {
<a name="line218"></a>    throw Error(&#39;file name invalid: &#39; + originalFileName);
<a name="line219"></a>  }
<a name="line220"></a>  if (sanitizedFileName.length &lt;= goog.gears.MAX_FILE_NAME_LENGTH_) {
<a name="line221"></a>    return sanitizedFileName;
<a name="line222"></a>  }
<a name="line223"></a>
<a name="line224"></a>  // Keep as many characters in original as we can, then hash the rest.
<a name="line225"></a>  var prefix = sanitizedFileName.substring(
<a name="line226"></a>      0, goog.gears.MAX_FILE_NAME_PREFIX_LENGTH_);
<a name="line227"></a>  return prefix + String(goog.string.hashCode(originalFileName));
<a name="line228"></a>};
</pre>


</body>
</html>
